import { DownloadOutlined } from '@ant-design/icons';
import { SfwButton } from '@sfkj/sfw-dc-ui';
import { App, Modal, ModalProps } from 'antd';
import React from 'react';
import { aopUtils } from '../../utils';
import SfwFilePreviewContent from '../SfwFilePreviewContent';

export type UploadFileType = {
  url: string;
  name: string;
  size: number;
  type: string;
};

export type SfwFilePreviewProps = {
  file?: UploadFileType;
  contentProps?: any;
} & ModalProps;

const SfwFilePreview: React.FC<SfwFilePreviewProps> = (props) => {
  const { file, contentProps, ...restProps } = props;
  const { message } = App.useApp();

  const handleDownloadClick = async (url: string) => {
    const result = await aopUtils.downloadFile(url);

    if (result.error) {
      message.error(`下载失败，请稍后再试`);
      return;
    }
  };

  return (
    <Modal
      title={`预览《${file?.name || `未知文件`}》`}
      width={'100%'}
      styles={{
        body: {
          height: 'calc(100vh - 150px)',
        },
      }}
      centered
      footer={
        <>
          {file?.url && (
            <SfwButton
              icon={<DownloadOutlined />}
              type="primary"
              onClick={() => handleDownloadClick(file.url)}
            >
              {/*<a
                rel='noreferrer'
                href={`${file.url}?response-content-type=application%2Foctet-stream`}
                // href={`${getUrl(file.url)}`}
                download
                target='_blank'
              >
                下载文件
              </a>*/}
              下载文件
            </SfwButton>
          )}
          <SfwButton onClick={(e) => restProps.onCancel?.(e as any)}>
            关闭预览
          </SfwButton>
        </>
      }
      {...restProps}
    >
      <SfwFilePreviewContent file={file} {...contentProps} />
    </Modal>
  );
};

export default SfwFilePreview;
